/**
 * Created with IntelliJ IDEA.
 * Description:搜索插入位置
 * Date: 2024-02-04
 * Time:18:01
 */
public class Solution {
    public int searchInsert(int[] nums, int target) {
        //表示中点下标
        int len = nums.length;
        int mid = len/2;
        int left = 0;
        int right = len-1;
        while(right >= left) {
            if(nums[mid] < target) {
                left = mid + 1;
                mid = (left + right)/2;
            } else if(nums[mid] > target) {
                right = mid - 1;
                mid = (left + right)/2;
            } else {
                return mid;
            }
        }
        return right + 1;
        /*
        while(mid < len-1) {
            //比较target和mid的大小
            if (mid < target) {
                mid = (mid + len) / 2;
            } else if(mid > target) {
                mid /= 2;
            } else {
                //相等  就返回下标
                return mid;
            }
        }
        //单独验证最后一个元素是否就是target
        if(nums[len-1] == target) {
            return len-1;
        } else {
            //找不到target
            return len;
        }
*/
    }
/////////////////////////////////////////////////
    //螺旋矩阵
    public int[][] generateMatrix(int n) {
        int[][] ret = new int[n][n];
        int num = 1;
        int line = 0;
        int arrange = 0;

            //左闭右闭
/*
            for (int i = 0; i < n-1; i++) {
                ret[line][arrange] = num;
                num++;
                arrange++;
            }
            for(int i = 0;i < n-1;i++) {
                ret[line][arrange] = num;
                num++;
                line++;
            }
            for(int i = 0;i < n-1)
*/
            for (int j = n - 2; j > 0; j--) {
                for (int i = j; i >=0; i--) {
                    ret[line][arrange] = num;
                    num++;
                    arrange++;

                    ret[line][arrange] = num;
                    num++;
                    line++;

                    ret[line][arrange] = num;
                    num++;
                    arrange--;

                    ret[line][arrange] = num;
                    num++;
                    line--;
                }
            }

    }

}
